// mircoApp/competitionApply/studentManage/studentManage.js
const app = getApp();
import Dialog from '../../../dist/dialog/dialog';
const skinBehavior = require("../../../utils/themeSkin/skinBehavior");
Page({
  behaviors: [skinBehavior],
  /**
   * 页面的初始数据
   */
  data: {
    searchValue:'',
    query:{
      student_keyword:'',
      page:1
    },
    totalPage:0,
    isLoadMore: true,
    studentArr:[

    ],
    iPnoneBottomBol: false, //是否是苹果手机
    coThemeColor:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    
  },
  getiPnoneBottom() {
    let that = this;
    try {
      let value = wx.getStorageSync('iPhoneBottom');
      if (value) {
        that.setData({
          iPnoneBottomBol: true
        });
        // Do something with return value
      }
    } catch (e) {
      // Do something when catch error
    }
  },
  colorFin(a) { //处理主题色
    a = a.substring(1);
    a = a.toLowerCase();
    let b = new Array();
    for (let x = 0; x < 3; x++) {
      b[0] = a.substr(x * 2, 2);
      b[3] = "0123456789abcdef";
      b[1] = b[0].substr(0, 1);
      b[2] = b[0].substr(1, 1);
      b[20 + x] = b[3].indexOf(b[1]) * 16 + b[3].indexOf(b[2]);
    }
    return b[20] + "," + b[21] + "," + b[22];
  },
  initData() {
    this.setData({
      'query.student_keyword':'',
      'query.page':1,
      isLoadMore:true,
      studentArr:[]
    })
  },
  onSearch(event) {
    console.log(event);
    this.setData({
      'query.student_keyword':event.detail,
      'query.page':1,
      isLoadMore:true,
      studentArr:[]
    })
    this.goodsList();
  },
  onClear(event) {
    console.log(event);
    this.setData({
      'query.student_keyword':'',
      'query.page':1,
      isLoadMore:true,
      competitionArr:[]
    })
    this.goodsList();
  },
  studentDel(event) {
    console.log(event);
    let {id} = event.detail;
    Dialog.confirm({
      // title: '提示',
      message: '是否确定删除该学员？',
    }).then(() => {
      // on close
      let urlStr = app.getNetAddresss("plugin.event-registration.api.student.delete");
      app._postNetWork({
        url: urlStr,
        data: {id:id},
        success: (resdata) => {
          var res = resdata.data;
          let data = res.data;
          if (res.result == 1) {
            app.tips(res.msg);
            this.initData();
            this.goodsList();
          }
        },
        fail: function (res) {
          console.log(res);
        },
      });
    })
    .catch(() => {
      // on cancel
    });  //删除订单
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  stuentAdd() {
    wx.navigateTo({
      url:'/mircoApp/competitionApply/actionStudent/actionStudent'
    })
    //mircoApp/competitionApply/applyForm/applyForm
  },
  goodsList() {
    let urlStr = app.getNetAddresss("plugin.event-registration.api.student.get-list");
    app._postNetWork({
      url: urlStr,
      data:this.data.query,
      success:  (resdata) => {
        let res = resdata.data;
        let data = res.data;
        wx.hideLoading();
        wx.hideNavigationBarLoading();//隐藏导航条加载动画
        //停止下拉刷新
        wx.stopPullDownRefresh();
        if (res.result == 1) {
          console.log(data);
          this.data.totalPage = data.last_page;
          if (!this.data.totalPage) {
            this.data.totalPage = 0;
          }
          this.setData({
            studentArr: data.data,
          })
          console.log(this.data.studentArr);
        } else {
          this.data.query.page = this.data.query.page - 1;
          this.data.isLoadMore = false;
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  //获取更多数据
  getMoreData() {
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.query.page >= this.data.totalPage) {
      return;
    } else {
      this.data.query.page = this.data.query.page + 1;
      let urlStr = app.getNetAddresss("plugin.event-registration.api.student.get-list");
      app._postNetWork({
        url: urlStr,
        data: this.data.query,
        success: (resdata) => {
          let res = resdata.data;
          let list = res.data.list?.data;
          console.log(list);
          if (res.result == 1) {
            this.data.isLoadMore = true;
            this.setData({
              studentArr : this.data.studentArr.concat(list),
            })
          } else {
            this.data.page = this.data.page - 1;
            this.isLoadMore = false;
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let yz_basic_info = wx.getStorageSync("yz_basic_info");
    if(yz_basic_info) {
      this.setData({
        coThemeColor:this.colorFin(yz_basic_info.theme_color)
      })
    }
    
    this.initData();
    this.getiPnoneBottom();
    this.goodsList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    wx.showNavigationBarLoading();
    //显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
    wx.showLoading({
     title: '刷新中...',
   })
   this.setData({
     'query.page':1,
     studentArr:[],
     isLoadMore: true,
   })
   this.goodsList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      console.log('没有更多了');
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})